package logc

import "text/template"

type PhpConfig struct {
	AppTag            string
	PlatformCookieKey string
	BusinessCookieKey string
	Object            []Object
	Operate           []Operate
}
type Object struct {
	Name string
	Id   int
	Desc string
}
type Operate struct {
	Name string
	Id   int
	Desc string
}

type LogResult struct {
	OutputStr string
}

func (r *LogResult) Write(p []byte) (n int, err error) {
	r.OutputStr += string(p)
	return len(r.OutputStr), nil
}

func BuildPhpConfig(data PhpConfig) (output string, err error) {
	t := template.New("phpconfig")
	phpConfTmp :=
		`
<?php

return [
	'neibu_url' => env('LOGC_NEIBU_URL', 'http://logcneibu.xxx.com'),
	'app_tag' => '{{.AppTag}}',
	'platform_cookie_key' => '{{.PlatformCookieKey}}', // 平台号cookie key
	'business_cookie_key' => '{{.BusinessCookieKey}}', // 业务方cookie key
	'object' => [
{{with .Object}}
{{range .}}
		'{{.Name}}' => {{.Id}}, // {{.Desc}}
{{end}}
{{end}}
	],
	'operate' => [
{{with .Operate}}
{{range .}}
		'{{.Name}}' => {{.Id}}, // {{.Desc}}
{{end}}
{{end}}
    ],
];
`
	t, err = t.Parse(phpConfTmp)
	if err != nil {
		return
	}
	result := &LogResult{}
	err = t.Execute(result, data)
	if err != nil {
		return
	}
	return result.OutputStr, err
}
